package com.zhangquan.framework.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
/*import org.springframework.web.servlet.config.annotation.InterceptorRegistry;*/
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class ResourcesConfig implements WebMvcConfigurer {
	
	
	/**
	 * 添加拦截器
	 */
	/*@Override
	public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
    }*/
	
	/**
	 * 本地文件上传路径
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {	
		// win系统,linux系统 创建不同路径
    	String os = System.getProperty("os.name");    	
    	if(os.toLowerCase().startsWith("win")){  
    		registry.addResourceHandler("/profile/**").addResourceLocations("file:D:/usr/local/profile/"); 	
    	} else {	
    		registry.addResourceHandler("/profile/**").addResourceLocations("file:/usr/local/profile/");
    	}
           
    }
	
	/**
	 * web跨域访问配置
	 */
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		// 设置允许跨域的路径
		registry.addMapping("/**")
				// 设置允许跨域请求的域名
				.allowedOrigins("*")
				// 是否允许证书
				.allowCredentials(true)
				// 设置允许的方法
				.allowedMethods("GET", "POST", "DELETE", "PUT")
				// 设置允许的header属性
				.allowedHeaders("*")
				// 跨域允许时间
				.maxAge(3600);
	}

}
